package Jeux;

import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

public class Joystick {

	boolean boundJoytrick=false;
	Controller firstJoyStick;
	double sensitivity;
	Component[] buttons;

	public Joystick(){
		firstJoyStick=null;
		Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
		for(int i=0;i<controllers.length && firstJoyStick==null;i++) {  
			if(controllers[i].getType()==Controller.Type.STICK) {
				firstJoyStick = controllers[i];
				boundJoytrick=true;
				buttons = firstJoyStick.getComponents();
			}
		}
		sensitivity=0.2;
	}


	public Joystick(double sensitivity){
		firstJoyStick=null;
		Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
		for(int i=0;i<controllers.length && firstJoyStick==null;i++) {  
			if(controllers[i].getType()==Controller.Type.STICK) {
				firstJoyStick = controllers[i];
				boundJoytrick=true;
				buttons = firstJoyStick.getComponents();
			}
		}
		this.sensitivity=sensitivity;
	}

	public boolean isUp(){
		return (boundJoytrick && !(buttons[0].getPollData()>-sensitivity && buttons[0].getPollData()>-1.5));
	}

	public boolean isDown(){
		return (boundJoytrick && !(buttons[0].getPollData()<sensitivity && buttons[0].getPollData()>-1.5));
	}

	public boolean isLeft(){
		return (boundJoytrick && !(buttons[1].getPollData()>-sensitivity && buttons[1].getPollData()>-1.5));
	}

	public boolean isRight(){
		return (boundJoytrick && !(buttons[1].getPollData()<sensitivity && buttons[1].getPollData()>-1.5));
	}

	public boolean isButton1(){
		return (boundJoytrick &&  buttons[3].getPollData()==0.1f);
	}

	public boolean isButton2(){
		return  (boundJoytrick && buttons[4].getPollData()==0.1f);
	}

	public boolean isButton3(){
		return  (boundJoytrick && buttons[5].getPollData()==0.1f);
	}

	public static void main(String[] args) {
		Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
		Controller firstMouse=null;
		for(int i=0;i<controllers.length && firstMouse==null;i++) {  
			if(controllers[i].getType()==Controller.Type.STICK) {
				// Found a mouse
				firstMouse = controllers[i];
			}
		}
		if(firstMouse==null) {
			// Couldn't find a mouse
			System.out.println("Found no mouse");
			System.exit(0);
		}

		System.out.println("First mouse is: " + firstMouse.getName());

		while(true) {
			firstMouse.poll();
			Component[] components = firstMouse.getComponents();
			StringBuffer buffer = new StringBuffer();
			for(int i=0;i<components.length;i++) {
				if(i>0) {
					buffer.append(", ");
				}
				buffer.append(components[i].getName());
				buffer.append(": ");
				if(components[i].isAnalog()) {
					buffer.append(components[i].getPollData());
				} else {
					if(components[i].getPollData()==1.0f) {
						buffer.append("On");
					} else {
						buffer.append("Off");
					}
				}
			}
			System.out.println(buffer.toString());

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
